define(['knockout', 'eventbus'], function (ko, eventbus) {
	return function (params) {
		// Observe the urls
		let firstUrl = ko.observable();
		let prevUrl = ko.observable();
		let nextUrl = ko.observable();
		let lastUrl = ko.observable();
		let currentUrl = ko.observable();

		// Observe the enabled flag of buttons.
		let navigable = ko.observable(false);
		let firstEnabled = ko.observable(false);
		let hasPrevious = ko.observable(false);
		let hasNext = ko.observable(false);
		let lastEnabled = ko.observable(false);

		// Determine if we have multiple pages or just one.
		// For showing / hiding the whole navigation.
		let canNavigate = function () {
			return firstUrl() !== lastUrl();
		};

		// Click callback for the first button.
		let first = function () {
			params.getItems(firstUrl());
		}

		// Click callback for the previous button.
		let previous = function () {
			params.getItems(prevUrl());
		}

		// Click callback for the next button.
		let next = function () {
			params.getItems(nextUrl());
		}

		// Click callback for the last button.
		let last = function () {
			params.getItems(lastUrl());
		}

		// Check if we have a first page.
		let checkIfFirst = function () {
			return currentUrl() === firstUrl();
		}

		// Check if we have a previous page.
		let checkIfPrevious = function () {
			return prevUrl() !== null;
		}

		// Check if we have a next page.
		let checkIfNext = function () {
			return nextUrl() !== null;
		}

		// Check if we have a last page.
		let checkIfLast = function () {
			return currentUrl() === lastUrl();
		}

		// Listen to the event provided as parameter to this component.
		eventbus.subscribe(params.eventName, function (paginationData) {
			// Refresh the url data.
			firstUrl(paginationData.firstUrl);
			prevUrl(paginationData.prevUrl);
			nextUrl(paginationData.nextUrl);
			lastUrl(paginationData.lastUrl);
			currentUrl(paginationData.currentUrl);

			// Refresh the flag state.
			navigable(canNavigate());
			firstEnabled(!checkIfFirst());
			hasPrevious(checkIfPrevious());
			hasNext(checkIfNext());
			lastEnabled(!checkIfLast());
		});

		// Expose what is needed in the UI.
		return {
			navigable,
			firstEnabled,
			hasPrevious,
			hasNext,
			lastEnabled,
			first,
			previous,
			next,
			last
		}
	}
});